﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;


namespace OI.Controls
{
    public partial class TextMarker
    {
        public string Text { get; private set; }
        public double TextFontSize { get; private set; }
        public Brush TextForeground { get; private set; }
        public Action<string, double> FontSetAction { get; private set; }

        public TextMarker(Brush textForeground, Action<string, double> fontSetAction)
        {
            TextForeground = textForeground;
            FontSetAction = fontSetAction;

            InitializeComponent();
            TBox.Foreground = TextForeground;
        }


        public TextMarker(string text, double textFontSize, Brush textForeground)
        {
            Text = text;
            TextFontSize = textFontSize;
            TextForeground = textForeground;

            InitializeComponent();
            SetText();
        }


        private void TextBoxKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                Text = TBox.Text;
                TextFontSize = TBox.FontSize;
                SetText();
            }
        }


        private void SetText()
        {
            LayoutRoot.Children.Clear();

            var tb = new TextBlock
            {
                FontSize = TextFontSize,
                Foreground = TextForeground,
                Text = Text,
                Margin = new Thickness(2),
                Style = Application.Current.Resources["DrawnText"] as Style
            };

            LayoutRoot.Children.Add(tb);

            if (FontSetAction != null)
            {
                FontSetAction(Text, TextFontSize);
            }
        }


        private void OnSpin(object sender, SpinEventArgs e)
        {
            if (e.Direction == SpinDirection.Increase)
            {
                TBox.FontSize++;
            }
            else if (TBox.FontSize >= 6)
            {
                TBox.FontSize--;
            }
        }


        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            TBox.Focus();
        }
    }
}
